package com.kgis.fm.common.component.legend.control{
    import com.kgis.fm.common.component.legend.event.BusinessLegendCtrlEvent;
    
    import flash.events.Event;
    
    import mx.collections.ArrayCollection;
    import mx.controls.listClasses.BaseListData;
    import mx.controls.listClasses.IDropInListItemRenderer;
    import mx.controls.listClasses.IListItemRenderer;
    import mx.core.IDataRenderer;
    import mx.core.UIComponent;
    import mx.core.UITextField;
    import mx.events.FlexEvent;
    import mx.managers.ILayoutManagerClient;

    [Event(name="dataChange", type="mx.events.FlexEvent")]
    public class TreeGridChkItemRanderer extends UIComponent implements IDataRenderer, IDropInListItemRenderer, ILayoutManagerClient, IListItemRenderer {

        private static var _colorForThirdState:int = 0x37BEF8;

        /**
         *  Constructor.
         */
        public function TreeGridChkItemRanderer() {
            super();
        }

        public var HalfSelectIcon:Class;

        public var SelectedIcon:Class;

        public var UnSelectIcon:Class;

        /**
         * 样式使用
         */
        protected var _verticalGap:int = 4;
        protected var allSelect:Boolean;
        /**
         *  The internal IFlexDisplayObject that displays the disclosure icon
         *  in this renderer.
         */
        //protected var disclosureIcon:IFlexDisplayObject;

        //----------------------------------
        //  icon
        //----------------------------------

        /**
         *  The internal IFlexDisplayObject that displays the icon in this renderer.
         */
        //protected var icon:IFlexDisplayObject;

        //----------------------------------
        //  label
        //----------------------------------

        /**
         *  The internal UITextField that displays the text in this renderer.
         */
        protected var label:UITextField;
        private var _checked:Boolean = false;
        /**
         *  @private
         */
        private var _data:Object;

        /**
         *  @private
         */
        private var _listData:TreeGridListData;
        /**
           /**
         * 树节点的选择控件
         */
        private var checkBox:CenterCheckBox; //CheckBox 控件;
        /**
         *  @private
         */
        private var invalidatePropertiesFlag:Boolean = false;

        /**
         *  @private
         */
        private var invalidateSizeFlag:Boolean = false;

        /**
         *  The internal shape that displays the trunks in this renderer.
         */
        private var listOwner:TreeGrid;
        public function get Checked():Boolean {
            return checkBox.selected;
        }

        public function set Checked(value:Boolean):void {
            checkBox.selected = value;
        }

        /**
         * 字段默认 isTreeSelected 但可以自定义的
         * @param selectedField
         *
         */
        public function set _selectedField(selectedField:String):void {
            this._selectedField = selectedField;
        }

        [Bindable("dataChange")]
        public function get data():Object {
            return _data;
        }

        public function set data(value:Object):void {
            _data = value;
            invalidateProperties();
            dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
        }

        override public function invalidateProperties():void {
            super.invalidateProperties();
        }

        [Bindable("dataChange")]
        public function get listData():BaseListData {
            return _listData;
        }

        public function set listData(value:BaseListData):void {
            if (!value)
                return;
            _listData = TreeGridListData(value);

        }

        /**
         *  @private
         */
        override public function set nestLevel(value:int):void {
            super.nestLevel = value;
        }

        /**
         *  水平间距设置器，本应是Horizonal的，但有些字段重构麻烦
         * @param gap
         *
         */
        public function set verticalGap(gap:int):void {
            this._verticalGap = gap;
        }

        /**
         * 点击checkbox时,更新dataProvider
         * @param event
         */
        protected function changeHandler(event:Event):void {
            var changevent:BusinessLegendCtrlEvent = new BusinessLegendCtrlEvent(BusinessLegendCtrlEvent.TREENODE_CHECKCHANGED);
            var legendItem:LegendDisplayItem = _data.legendItem;
            if (this._listData.dataField == "selectable") {
                changevent.legendItemName = "selectable";
                //_data[_LegendselectedField]=checkBox.selected;
                legendItem.selectable = checkBox.selected;
                legendItem.s_childrenSelect = false;
            }
            if (this._listData.dataField == "visible") {
                changevent.legendItemName = "visible";
                //_data[_MarkselectedField]=checkBox.selected;
                legendItem.visible = checkBox.selected;
                legendItem.v_childrenSelect = false;
            }
            if (this._listData.dataField == "levelable") {
                changevent.legendItemName = "levelable";
                legendItem.levelable = checkBox.selected;
                legendItem.l_childrenSelect = false;
            }
            toggleChildren(_data, changevent.legendItemName, checkBox.selected);
            toggleParent(legendItem, changevent.legendItemName);

            if (this._listData.dataField == "selectable") {
                changevent.bIsChecked = legendItem.selectable;
            }
            if (this._listData.dataField == "visible") {
                changevent.bIsChecked = legendItem.visible;
            }
            if (this._listData.dataField == "levelable") {
                changevent.bIsChecked = legendItem.levelable;
            }
            changevent.legendItem = _data.legendItem;
            changevent.sender = this;
            listOwner.dispatchEvent(changevent);
            invalidateCheckBox();
            invalidateProperties();
        }

        /**
         *  @private
         */
        override protected function commitProperties():void {
            super.commitProperties();
            if (_data) {
                var legendItem:LegendDisplayItem = _data.legendItem;
                listOwner = TreeGrid(_listData.owner);
                if (this._listData.dataField == "visible") {
                    checkBox.selected = legendItem.visible;
                    if (legendItem.layer) {
                        toggleChildren(_data, "visible", legendItem.visible);
                        toggleParent(legendItem, "visible");
                        fillCheckBox(true);
                    }
                    else {
                        if (_data.children) {
                            var legendItem1:LegendDisplayItem = _data.legendItem;
                            if (legendItem1.v_childrenSelect == true)
                                fillCheckBox(true);
                            else
                                fillCheckBox(false);
                            legendItem1.open = true;
                        }
                        else
                            fillCheckBox(true);
                    }
                }
                if (this._listData.dataField == "selectable") {
                    checkBox.selected = legendItem.selectable;
                    if (legendItem.layer) {
                        toggleChildren(_data, "selectable", legendItem.selectable);
                        toggleParent(legendItem, "selectable");
                        fillCheckBox(true);
                    }
                    else {
                        if (_data.children) {
                            var legendItem2:LegendDisplayItem = _data.legendItem;
                            if (legendItem2.s_childrenSelect == true)
                                fillCheckBox(true);
                            else
                                fillCheckBox(false);
                            legendItem2.open = true;
                        }
                        else
                            fillCheckBox(true);
                    }
                }
                if (this._listData.dataField == "levelable") {
                    checkBox.selected = legendItem.levelable;
                    if (legendItem.layer) {
                        toggleChildren(_data, "levelable", legendItem.levelable);
                        toggleParent(legendItem, "levelable");
                        fillCheckBox(true);
                    }
                    else {
                        if (_data.children) {
                            var legendItem3:LegendDisplayItem = _data.legendItem;
                            if (legendItem3.l_childrenSelect == true)
                                fillCheckBox(true);
                            else
                                fillCheckBox(false);
                            legendItem3.open = true;
                        }
                        else
                            fillCheckBox(true);
                    }
                }
            }
            else {
                //label.text = " ";
                checkBox.label = " ";
            }
            invalidateDisplayList();
        }
        /**
         *  @private
         */
        override protected function createChildren():void {
            super.createChildren();
            checkBox = new CenterCheckBox();
            addChild(checkBox);
            checkBox.addEventListener(Event.CHANGE, changeHandler);
//            addEventListener(ToolTipEvent.TOOL_TIP_SHOW, toolTipShowHandler);
        }

        /**
         *  @private
         */
        protected function invalidateCheckBox():void {
            if (this._data.children) {
                if (this._listData.dataField == "selectable") {
                    var legendItem:LegendDisplayItem = _data.legendItem;
                    if (legendItem.s_childrenSelect == true)
                        fillCheckBox(true);
                    else
                        fillCheckBox(false);
                }
                if (this._listData.dataField == "visible") {
                    var legendItem1:LegendDisplayItem = _data.legendItem;
                    if (legendItem1.v_childrenSelect == true)
                        fillCheckBox(true);
                    else
                        fillCheckBox(false);
                }
                if (this._listData.dataField == "levelable") {
                    var legendItem2:LegendDisplayItem = _data.legendItem;
                    if (legendItem2.l_childrenSelect == true)
                        fillCheckBox(true);
                    else
                        fillCheckBox(false);
                }
            }
            else
                fillCheckBox(true);
        }

        /**
         *  @private
         */
        override protected function measure():void {
            super.measure();
            var w:Number = _data ? _listData.indent : 5;
            w = w + 5;
            if (checkBox.width < 4 || checkBox.height < 4) {
                checkBox.width = 4;
                checkBox.height = 16;
            }
            if (isNaN(explicitWidth)) {
                w += checkBox.getExplicitOrMeasuredWidth();
                measuredWidth = w;
                measuredHeight = checkBox.getExplicitOrMeasuredHeight();
            }
            else {
                measuredHeight = checkBox.getExplicitOrMeasuredHeight();
                var w1:Number = this.getExplicitOrMeasuredWidth();
                checkBox.x = (w1 - 4) / 2;
            }
        }

        /**
         *  @private
         */
        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
            super.updateDisplayList(unscaledWidth, unscaledHeight);
            var startx:Number = _data ? _listData.indent : 0;
        }

        private function checkOpen():Boolean {
            var isOpen:Boolean = false;
            var openitems:Array = listOwner.openItems as Array;
            for (var i:int = 0; i < openitems.length; i++) {
                var lstData:Object = openitems[i];
                if (lstData.name == _data.legendItem.name) {
                    isOpen = true;
                    break;
                }
            }
            if (isOpen == true) {
                return true;
            }
            else {
                if (_data.children.length > 0)
                    return false;
            }
            return true;
        }

        private function fillCheckBox(isFill:Boolean):void {
            if (isFill == true) {
                checkBox.setStyle("symbolColor", "0x000000");  //修改演示
            }
            else {
                checkBox.setStyle("symbolColor", "0xc0c080");
            }
        }

        private function toggleChildren(item:Object, isSelectCol:String, beCheck:Boolean, iLoop:int = 0):void {
            var object:Object;
            if (!item)
                return;
            else {
                if (null == item.children)
                    return;
                var openitems:ArrayCollection = item.children as ArrayCollection;
                for (var i:int = 0; i < openitems.length; i++) {
                    var lstData:Object = openitems[i];
                    var lstDataItem:LegendDisplayItem = lstData.legendItem;
                    if ("visible" == isSelectCol) {
                        lstDataItem.visible = beCheck;
                    }
                    if ("selectable" == isSelectCol) {
                        lstDataItem.selectable = beCheck;
                    }
                    if ("levelable" == isSelectCol) {
                        lstDataItem.levelable = beCheck;
                    }
                    if (lstData.children) {
                        for (var j:Number = 0; j < lstData.children.length; j++) {
                            var nextItem:LegendDisplayItem = lstData.children[j].legendItem;
                            if (lstData.children.length == 1) {
                                if ("visible" == isSelectCol) {
                                    nextItem.visible = beCheck;
                                }
                                if ("selectable" == isSelectCol) {
                                    nextItem.selectable = beCheck;
                                }
                                if ("levelable" == isSelectCol) {
                                    nextItem.levelable = beCheck;
                                }
                                object = lstData.children[j];
                                toggleChildren(object, isSelectCol, beCheck, 1);
                            }
                            else {
                                if ("visible" == isSelectCol) {
                                    nextItem.visible = beCheck;
                                }
                                if ("selectable" == isSelectCol) {
                                    nextItem.selectable = beCheck;
                                }
                                if ("levelable" == isSelectCol) {
                                    nextItem.levelable = beCheck;
                                }
                                object = lstData.children[j];
                                toggleChildren(object, isSelectCol, beCheck, 1);
                            }
                        }
                    }
                    else {
                        if ("visible" == isSelectCol) {
                            (lstData.legendItem as LegendDisplayItem).visible = beCheck;
                        }
                        if ("selectable" == isSelectCol) {
                            (lstData.legendItem as LegendDisplayItem).selectable = beCheck;
                        }
                        if ("levelable" == isSelectCol) {
                            (lstData.legendItem as LegendDisplayItem).levelable = beCheck;
                        }
                    }
                }
            }
        }

        private function toggleParent(item:LegendDisplayItem, isSelectCol:String):void {
            var BChange:Boolean = false;
            if (!item)
                return;
            else {
                if (item.parentLegendItem) {
                    var parentItem:LegendDisplayItem = item.parentLegendItem;
                    var openitems:Array = listOwner.openItems as Array;
                    for (var i:int = 0; i < openitems.length; i++) {
                        var lstData:Object = openitems[i];
                        if (lstData.name == parentItem.name) {
                            if (parentItem.parentLegendItem == null) {
                                topparentItemHandler(lstData, isSelectCol);
                                return;
                            }
                            for (var j:int = 0; j < lstData.children.length; j++) {
                                if (lstData.children.length == 1) {
                                    if ("visible" == isSelectCol) {
                                        parentItem.visible = item.visible;
                                        if (item.v_childrenSelect == true)
                                            parentItem.v_childrenSelect = true;
                                        else
                                            parentItem.v_childrenSelect = false;
                                    }
                                    if ("selectable" == isSelectCol) {
                                        parentItem.selectable = item.selectable;
                                        if (item.s_childrenSelect == true)
                                            parentItem.s_childrenSelect = true;
                                        else
                                            parentItem.s_childrenSelect = false;
                                    }
                                    if ("levelable" == isSelectCol) {
                                        parentItem.levelable = item.levelable;
                                        if (item.l_childrenSelect == true)
                                            parentItem.l_childrenSelect = true;
                                        else
                                            parentItem.l_childrenSelect = false;
                                    }
                                    toggleParent(parentItem, isSelectCol);
                                    parentItem.open = true;
                                    break;
                                }
                                else {
                                    var nextItem:LegendDisplayItem = lstData.children[j].legendItem;
                                    if (nextItem.name != item.name) {
                                        if ((nextItem.layer == item.layer) && (null != item.layer)) {
                                            if (item.labelLayer == false) { //选择符号图层
                                                if ("visible" == isSelectCol) {
                                                    parentItem.visible = item.visible;
                                                    if (nextItem.visible == true)
                                                        parentItem.v_childrenSelect = true;
                                                    else
                                                        parentItem.v_childrenSelect = false;
                                                }
                                                if ("selectable" == isSelectCol) {
                                                    parentItem.selectable = item.selectable;
                                                    if (nextItem.selectable == true)
                                                        parentItem.s_childrenSelect = true;
                                                    //parentItem.s_childrenSelect=true;
                                                    else
                                                        parentItem.v_childrenSelect = false;
                                                }
                                                if ("levelable" == isSelectCol) {
                                                    parentItem.levelable = item.levelable;

                                                    if (nextItem.levelable == true)
                                                        parentItem.l_childrenSelect = true;
                                                    else
                                                        parentItem.l_childrenSelect = false;
                                                }
                                            }
                                            else //选择标注图层
                                            {
                                                if ("visible" == isSelectCol) {
                                                    parentItem.visible = nextItem.visible;
                                                    if (nextItem.visible == true)
                                                        parentItem.v_childrenSelect = true;
                                                    else {
                                                        item.visible = nextItem.visible;
                                                        parentItem.v_childrenSelect = false;
                                                    }
                                                }
                                                if ("selectable" == isSelectCol) {
                                                    item.selectable = nextItem.selectable;
                                                    parentItem.selectable = nextItem.selectable;
                                                    if (nextItem.selectable == true)
                                                        parentItem.s_childrenSelect = true;
                                                    //parentItem.s_childrenSelect=true;
                                                    else {
                                                        parentItem.v_childrenSelect = false;
                                                    }
                                                }
                                                if ("levelable" == isSelectCol) {
                                                    item.levelable = nextItem.levelable;
                                                    parentItem.levelable = nextItem.levelable;
                                                    if (nextItem.levelable == true)
                                                        parentItem.l_childrenSelect = true;
                                                    else {
                                                        parentItem.l_childrenSelect = false;
                                                    }
                                                }
                                                return;
                                            }
                                        }
                                        else {
                                            if (nextItem.layer == item.layer) {
                                                if ("visible" == isSelectCol) {
                                                    if (item.visible == true) {
                                                        parentItem.visible = item.visible;
                                                        if (nextItem.visible == true)
                                                            parentItem.v_childrenSelect = true;
                                                        else
                                                            parentItem.v_childrenSelect = false;
                                                    }
                                                    else {
                                                        if (nextItem.visible == item.visible)
                                                            parentItem.visible = item.visible;
                                                        parentItem.v_childrenSelect = false;
                                                    }
                                                }
                                                if ("selectable" == isSelectCol) {
                                                    if (item.selectable == true) {
                                                        parentItem.selectable = item.selectable;
                                                        if (nextItem.selectable == true)
                                                            parentItem.s_childrenSelect = true;
                                                        else
                                                            parentItem.v_childrenSelect = false;
                                                    }
                                                    else {
                                                        if (nextItem.selectable == item.selectable)
                                                            parentItem.selectable = item.selectable;
                                                        parentItem.s_childrenSelect = false;
                                                    }
                                                }
                                                if ("levelable" == isSelectCol) {
                                                    if (item.levelable == true) {
                                                        parentItem.levelable = item.levelable;
                                                        if (nextItem.levelable == true)
                                                            parentItem.l_childrenSelect = true;
                                                        else
                                                            parentItem.v_childrenSelect = false;
                                                    }
                                                    else {
                                                        if (nextItem.levelable == item.levelable)
                                                            parentItem.levelable = item.levelable;
                                                        parentItem.l_childrenSelect = false;
                                                    }
                                                }
                                            }
                                        }
                                        parentItem.open = true;
                                        toggleParent(parentItem, isSelectCol);
                                        break;
                                    }

                                }
                            }
                        }
                    }
                }
            }
        }

		private function topparentItemHandler(item:Object, isSelectCol:String):void {
            var beCheck:Boolean = false;
            var nextItem:LegendDisplayItem;
            for (var j:int = 0; j < item.children.length; j++) {
                nextItem = item.children[j].legendItem;
                if ("visible" == isSelectCol) {
                    if (true == nextItem.visible) {
                        beCheck = true;
                        break;
                    }
                }
                if ("selectable" == isSelectCol) {
                    if (true == nextItem.selectable) {
                        beCheck = true;
                        break;
                    }
                }
                if ("levelable" == isSelectCol) {
                    if (true == nextItem.levelable) {
                        beCheck = true;
                        break;
                    }
                }
            }
            if ("visible" == isSelectCol) {
                (item.legendItem as LegendDisplayItem).visible = beCheck;
            }
            if ("selectable" == isSelectCol) {
                (item.legendItem as LegendDisplayItem).selectable = beCheck;
            }
            if ("levelable" == isSelectCol) {
                (item.legendItem as LegendDisplayItem).levelable = beCheck;
            }
            if (beCheck == true)
                (item.legendItem as LegendDisplayItem).v_childrenSelect = true;
            else
                (item.legendItem as LegendDisplayItem).v_childrenSelect = false;
        }
    }
}